//
//  MMAlertInputTextView.m
//  MemeMaker
//
//  Created by dangthan on 7/16/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMAlertInputTextView.h"

@implementation MMAlertInputTextView
@synthesize textInput = _textInput;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (id) initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles {
    if (self = [super initWithTitle:title message:@"\n\n" delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil]) {
        [self addSubview:self.textInput];
        
        _tbvAlert = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
		_tbvAlert.delegate = self;
		_tbvAlert.dataSource = self;
		_tbvAlert.scrollEnabled = NO;
		_tbvAlert.opaque = NO;
		_tbvAlert.editing = YES;
		_tbvAlert.rowHeight = 28.0f;
		[self addSubview:_tbvAlert];
        
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
		[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    }
    return self;
}

- (void)layoutSubviews {
	// We assume keyboard is on.
	if ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications]) {
		if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
			self.center = CGPointMake(160.0f, (460.0f - 216.0f)/2 + 12.0f);
			_tbvAlert.frame = CGRectMake(12.0f, 51.0f, 260.0f, 28.0f);
		}
        else {
			self.center = CGPointMake(240.0f, (300.0f - 162.0f)/2 + 12.0f);
			_tbvAlert.frame = CGRectMake(12.0f, 35.0f, 260.0f, 28.0f);
		}
	}
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *AlertPromptCellIdentifier = @"AlertIdentifier";
    
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:AlertPromptCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:AlertPromptCellIdentifier];
        [cell.contentView addSubview:_textInput];
    }
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}

- (void)orientationDidChange:(NSNotification *)notification {
	[self setNeedsLayout];
}

- (UITextField *) textInput {
    if (!_textInput) {
		_textInput = [[UITextField alloc] initWithFrame:CGRectMake(5.0f, 0.0f, 255.0f, 28.0f)];
		_textInput.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
		_textInput.clearButtonMode = UITextFieldViewModeWhileEditing;
		_textInput.placeholder = @"Your text";
	}
	return _textInput;
}

//do not call super dealloc
- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
